#pragma once
#include<vector>
#include<functional>
#include<iostream>
#include<assert.h>
using namespace std;

namespace My
{

    template<class T>
    class Less
    {
    public:
        bool operator()(const T& x, const T& y)
        {
            return x < y;
        }
    };
    
    template<class T>
    class Less<T*>
    {
    public:
        bool operator()( T* &x, T* &y)
        {
            return *x < *y;
        }
    };


    template<class T>
    class Greater
    {
    public:
        bool operator()(const T& x, const T& y)
        {
            return x > y;
        }
    };
    template<class T>
    class Greater<T*>
    {
    public:
        bool operator()(const T*& x, const T*& y)
        {
            return *x > *y;
        }
    };
    

    template <class T, class Container = vector<T>, class Compare = Less<T>>
    class priority_queue
    {

    public:
        priority_queue()
        {}

        template <class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
            :_c(first, last)
        {
            for (int i = (_c.size() - 2) / 2; i >= 0; i--)
            {
                adjust_down(i);
            }
        }


        void adjust_down(int parent)
        {
            size_t child = parent * 2 + 1;
            while (child < _c.size())
            {
                if (child+1 < _c.size() && _comp(_c[child], _c[child+1]))
                {
                    child++;
                }

                if (_comp(_c[parent], _c[child]))
                {
                    swap(_c[child], _c[parent]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else
                {
                    break;
                }
            }
        }

        void adjust_up(int child)
        {
            int parent = (child - 1) / 2;
            while (child > 0)
            {
                if (_comp(_c[parent], _c[child]))
                {
                    swap(_c[child], _c[parent]);
                    child = parent;
                    parent = (child - 1) / 2;
                }
                else
                {
                    break;
                }
            }
        }

        bool empty() const
        {
            return _c.empty();
        }
        size_t size() const
        {
            return _c.size();
        }
        T top() const
        {
            assert(!empty());
            return _c[0];
        }
        void push(const T& x)
        {
            _c.push_back(x);
            adjust_up(_c.size() - 1);
        }

        void pop()
        {
            assert(!empty());
            swap(_c[0], _c[_c.size() - 1]);
            _c.pop_back();
            adjust_down(0);
        }
    private:
        Container _c;
        Compare _comp;
    };


    void test1()
    {
        priority_queue<int> q;

        q.push(1);
        q.push(2);
        q.push(3);
        q.push(4);

        while (!q.empty())
        {
            cout << q.top() << endl;
            q.pop();
        }

    }


    void test2()
    {
        vector<int >  v = {1, 23, 123, 99, 77};

        priority_queue<int> q(v.begin(), v.end());

        while (!q.empty())
        {
            cout << q.top() << endl;
            q.pop();
        }
    }


    void test3()
    {

        vector<int >  v = { 1, 23, 123, 99, 77 };

        priority_queue<int,vector<int>,Greater<int>> q(v.begin(), v.end());

        while (!q.empty())
        {
            cout << q.top() << endl;
            q.pop();
        }
    }
    
    void test4()
    {
    

        priority_queue<int*> q;
        q.push(new int(1));
        q.push(new int(2));
        q.push(new int(3));
        q.push(new int(4));


        while (!q.empty())
        {
            cout << *(q.top()) << endl;
            q.pop();
        }
    }
};